function Promise(executor) {
    // 添加属性
    this.PromiseState = 'pending';
    this.PromiseResult = null;
    this.callback = [];

    const self = this;
    // resolve 函数
    function resolve(data) {
        if(self.PromiseState !== 'pending') return;
        // 1、修改对象的状态
        self.PromiseState = 'fulfilled';
        // 2、设置对象结果值
        self.PromiseResult = data;
        // 当执行reslove的时候，如果有保存的回调，这时候再调用
        setTimeout(()=>{
            self.callback.forEach(item => {
                item.onResolved(data)
            })
        })
    }

    // resject 函数
    function reject(data) {
        if(self.PromiseState !== 'pending') return;
        // 1、修改对象的状态
        self.PromiseState = 'rejected';
        // 2、设置对象结果值
        self.PromiseResult = data;
        // 当执行reslove的时候，如果有保存的回调，这时候再调用
        setTimeout(()=>{
            self.callback.forEach(item => {
                item.onRejected(data)
            })
        })
    }

    try{
        executor(resolve, reject)
    }catch (e) {
        reject(e)
    }
}

// 添加 then方法
Promise.prototype.then = function (onResolved, onRejected) {
    const self = this;

    // 实现promise的异常穿透，兜底的catch处理异常
    if(typeof onRejected !== 'function'){
        onRejected = reason => {
            throw reason
        }
    }

    // 实现直传
    if(typeof onResolved !== 'function'){
        onResolved = value => {
            return value
        }
    }
    return new Promise((resolve , reject)=>{
        // 提取重复打码
        function callback(type){
            try{
                let result = type(self.PromiseResult)
                if(result instanceof Promise){
                    // 是pormise的对象
                    result.then(v=>{
                        resolve(v)
                    },r => {
                        reject(r)
                    })
                }else{
                    // 如果是非promise类型的对象
                    resolve(result)
                }
            }catch (e) {
                reject(e)
            }
        }

        // 调用回调函数 根据PromiseState状态
        if(this.PromiseState == 'fulfilled'){
            setTimeout(()=>{
                callback(onResolved)
            })
        }

        if(this.PromiseState == 'rejected'){
            setTimeout(()=>{
                callback(onRejected)
            })
        }

        // 如果是异步的，我们应该先把回调函数保存起来，等到状态改变的时候在调用
        if(this.PromiseState == 'pending'){
            this.callback.push({
                onResolved:function () {
                    callback(onResolved)
                },
                onRejected:function () {
                    callback(onRejected)
                }
            })
        }
    })
}

// 添加catch方法
Promise.prototype.catch = function (onRejected) {
    return this.then(undefined, onRejected)
}

// Promise的resolve
Promise.resolve = function (value) {
    return  new Promise((resolve, reject)=>{
        if(value instanceof Promise){
            value.then(v=>{
                resolve(v)
            },r=>{
                reject(r)
            })
        }else{
            resolve(value)
        }
    })
}


// Promise的 reject
Promise.reject = function (reason) {
    return new Promise((resolve, reject)=>{
        reject(reason)
    })
}

// Promise的 all
Promise.all = function (promises) {
    return new Promise((resolve, reject)=>{
        let count = 0;
        let arr =[];
        // 遍历
        for(let i=0;i<promises.length;i++){
            promise[i].then(v => {
                // 这一个对象的状态是成功
                // 计数变量 增加
                count++;
                // 保存当前的值
                arr[i] = v;
                // 判断
                if(count == promises.length){
                    // 修改状态
                    resolve(arr)
                }
            },r => {
                reject(r)
            })
        }
    })
}

// Promise的 race
Promise.race = function (promises) {
    return new Promise((resolve, reject)=>{
        // 遍历
        for(let i=0;i<promises.length;i++){
            promise[i].then(v => {
                // 修改状态
                resolve(v)
            },r => {
                reject(r)
            })
        }
    })
}

